%%
%问题四
luoju_xinde = 1.7; 
chushijiaodu = 16 * 2 * pi; 
%最初始旋转情况
jsiodufanwei = linspace(0, chushijiaodu, 1000); 
dingyingweizuobiao = luoju_xinde * jsiodufanwei / (2 * pi); % 计算对应的半径
% 初始化
lujingyi = 9; 
lujinger = lujingyi / 2; 

%进行假设
xiangqiedian1 = 0; 
theta_tangent2 = pi; 

%前
qianX = lujingyi * cos(xiangqiedian1 + jsiodufanwei);
qianY = lujingyi * sin(xiangqiedian1 + jsiodufanwei);

%后
houX = lujinger * cos(theta_tangent2 + jsiodufanwei);
houY = lujinger * sin(theta_tangent2 + jsiodufanwei);
%最初长度
arc1_length = 2 * pi * lujingyi / 4; % 第一段圆弧长度
arc2_length = 2 * pi * lujinger / 4; % 第二段圆弧长度
total_length = arc1_length + arc2_length;
disp(['第一段圆弧长度:', num2str(arc1_length), '米']);
disp(['第二段圆弧长度:', num2str(arc2_length), '米']);
disp(['总长度:', num2str(total_length), '米']);

% 模拟退火算法参数
chushiwendu = 100; 
zuizhongjieshu = 0.01;
jiangwen = 0.99; 
zuidacishu = 1000; 
goal = @(R1, R2) 2 * pi * (R1 / 4 + R2 / 4);
banjing1 = lujingyi;
banjing2 = lujinger;
qishiT = chushiwendu;
youzuiR1 = lujingyi;
youzuiR2 = lujinger;
jieguochangdu = goal(lujingyi, lujinger);

for i = 1:zuidacishu
    %随机
    new1zuobiao = banjing1 + (randn() * 0.1);
    new2zuobiao = banjing2 + (randn() * 0.1);
    
    %目标解
    chongxinjisuangchangdu = goal(new1zuobiao, new2zuobiao);
    
    %比较
    if chongxinjisuangchangdu < jieguochangdu || exp((jieguochangdu - chongxinjisuangchangdu) / qishiT) > rand
        banjing1 = new1zuobiao;
        banjing2 = new2zuobiao;
        youzuiR1 = new1zuobiao;
        youzuiR2 = new2zuobiao;
        jieguochangdu = chongxinjisuangchangdu;
    end
    
    %降温
    qishiT = qishiT * jiangwen;
    
    %终止
    if qishiT < zuizhongjieshu
        break;
    end
end

zuizhongyuanhuX1 = youzuiR1 * cos(xiangqiedian1 + jsiodufanwei);
zuizhongyuanhuY1 = youzuiR1 * sin(xiangqiedian1 + jsiodufanwei);

zuizhongyuanhuX2 = youzuiR2 * cos(theta_tangent2 + jsiodufanwei);
zuizhongyuanheY2 = youzuiR2 * sin(theta_tangent2 + jsiodufanwei);
%长度
arc1_length_optimized = 2 * pi * youzuiR1 / 4; 
arc2_length_optimized = 2 * pi * youzuiR2 / 4; 
total_length_optimized = arc1_length_optimized + arc2_length_optimized;
disp(['优化后的第一段圆弧长度:', num2str(arc1_length_optimized), '米']);
disp(['优化后的第二段圆弧长度:', num2str(arc2_length_optimized), '米']);
disp(['优化后的总长度:', num2str(total_length_optimized), '米']);